﻿var WF = (function (wf) {
    //TableName, KeyValue, WorkflowId, WorktaskId, WorktaskInsId, WorkflowInsId
    data = {
        Keyvalue: '',
        Tablename: '',
        WorkflowId: '',
        WorktaskId: '',
        WorkflowInsId: '',
        WorktaskInsId: '',
        OperatorInsId: ''
    };
    var _Tooltip = null; //任务信息
    var _TransmitName = ''; //转发姓名字符串

    //流程任务信息提示实体
    function Tip() {
        this.title = '';   //标题
        this.TaskToWhoMsg = '';  //任务信息
        this.ResultMsg = '';      //操作结果

    };
    wf.setTransmitName = function (Names) { WF._TransmitName = Names; }
    wf.NewTip = function () {
        var tip = new Tip();
        tip.title = '';
        tip.TaskToWhoMsg = '';
        tip.ResultMsg = '';
        return tip;
    };
    wf.init = function (tableName, workFlowId, workTaskId) {
        data.Tablename = tableName;
        data.WorkflowId = workFlowId;
        data.WorktaskId = workTaskId;
    };
    wf.init = function (tableName, workFlowId, workTaskId, workFlowInsId, worktaskInsId, operatorInsId) {
        data.Tablename = tableName;
        data.WorkflowId = workFlowId;
        data.WorktaskId = workTaskId;
        data.WorkflowInsId = workFlowInsId;
        data.WorktaskInsId = worktaskInsId;
        data.OperatorInsId = operatorInsId;
    };
    wf.setData = function (keyValue, tableName, workFlowId, workTaskId, workFlowInsId, worktaskInsId) {
        data.Keyvalue = keyValue;
        data.Tablename = tableName;
        data.WorkflowId = workFlowId;
        data.WorktaskId = workTaskId;

        data.WorkflowInsId = workFlowInsId;
        data.WorktaskInsId = worktaskInsId;
    };
    wf.setTooltip = function (tip) {
        _Tooltip = tip;
    };

    wf.setWorkflowInsId = function (workFlowInsId) {
        data.WorkflowInsId = workFlowInsId;
    };
    wf.setWorktaskInsId = function (worktaskInsId) {
        data.WorktaskInsId = worktaskInsId;
    };
    wf.setKeyvalue = function (keyValue) {
        data.Keyvalue = keyValue;
    };
    wf.setIsDraft = function (isdraft) {
        data.IsDraft = isdraft;
    };
    wf.setOperatorInsId = function (operatorInsId) {
        data.OperatorInsId = operatorInsId;
    };

    wf.getOperatorInsId = function () {
        var strOperatorInsId = '';
        if (data) {
            strOperatorInsId = data.OperatorInsId;
        }
        return strOperatorInsId;
    };

    //流程ID
    wf.getWorkflowId = function () {
        var strWorkflowId = '';
        if (data) {
            strWorkflowId = data.WorkflowId;
        }
        return strWorkflowId;
    };

    //    //草稿
    //    wf.getIsDraft = function () {
    //        var strIsDraft = '';
    //        if (data) {
    //            strIsDraft = data.IsDraft;
    //        }
    //        return strIsDraft;
    //    };

    //流程任务ID
    wf.getWorktaskId = function () {
        var strWorktaskId = '';
        if (data) {
            strWorktaskId = data.WorktaskId;
        }
        return strWorktaskId;
    };
    //流程实例ID
    wf.getWorkflowInsId = function () {
        var strWorkflowInsId = '';
        if (data) {
            strWorkflowInsId = data.WorkflowInsId;
        }
        return strWorkflowInsId;
    };
    //流程任务实例ID
    wf.getWorktaskInsId = function () {
        var strWorktaskInsId = '';
        if (data) {
            strWorktaskInsId = data.WorktaskInsId;
        }
        return strWorktaskInsId;
    };
    //务业表名
    wf.getTablename = function () {
        var strTablename = '';
        if (data) {
            strTablename = data.Tablename;
        }
        return strTablename;
    };
    //务业表主键
    wf.getKeyvalue = function () {
        var strKeyvalue = '';
        if (data) {
            strKeyvalue = data.Keyvalue;
        }
        return strKeyvalue;
    };
    //流程任务执行结果
    wf.getTooltip = function () {
        return _Tooltip;
    };
    return wf;
})(WF || {});

//流程起动
WF.Start = (function (start) {
    var _baseAPIUrl = "WorkflowDataManager.aspx/WorkFlowStatr"; //驱动流程起动
    if (window.location.pathname == '/crm/BasePages/StartWorkflow.aspx') { _baseAPIUrl = "WorkflowDataManager.aspx/WorkFlowStatr"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/WorkFlowStatr"; }
    var _currentParams = [];
    var _loaded = false;

    var _KeyValue = "";
    var _TableName = "";
    var _WorkflowId = '';
    var _WorktaskId = '';
    var _Title = "";
    var _IsDraft = ''; //是否草稿状态
    var _Name = "";
    var _Value = "";


    start.init = function (Title, xIsDraft, xName, xValue) {

        _KeyValue = WF.getKeyvalue();
        _TableName = WF.getTablename();
        _WorkflowId = WF.getWorkflowId();
        _WorktaskId = WF.getWorktaskId();
        _IsDraft = arguments[1];
        _Title = Title;
        _Name = xName;
        _Value = xValue;
        _loaded = false;

        WF.Start.open();
    };
    start.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            TableName: _TableName,
            KeyValue: _KeyValue,
            xWorkflowId: _WorkflowId,
            xWorktaskId: _WorktaskId,
            xTitle: _Title,
            IsDraft: _IsDraft
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            StartWorkflow(data);
        }
    };
    function StartWorkflow(resp) {

        if (resp.indexOf('OK%') >= 0) {
            var respArray = resp.split("@");
            if (respArray[0] == "OK%") {
               
                //alert("成功");
                if (typeof(_IsIE)!="undefined"&&_IsIE== '0') {
                    WF.Tooltip.init(WF._TransmitName, WF.getWorktaskInsId());
                } else {
                    window.location = "../crm/BasePages/tooltip.aspx?worktaskInstanceId=" + respArray[1] +" &r="+Math.random().toString();
                }
            } else {
                if (respArray[0] == "OK%123") {
                    alert("保存草稿成功!");
                }
                else {
                    alert("启动流程失败");
                }
            }
        }
        else {
            alert("启动流程失败");
        }
    };
    return start
})(WF.start || {});


//根据任务配置加载流程命令按钮
WF.CommandsButton = (function (commandsbutton) {
    var _baseAPIUrl = "WorkflowDataManager.aspx/GetCommandsButton"; //驱动流程起动
    var _currentParams = [];
    var _loaded = false;

    var _WorkflowId = '';
    var _WorktaskId = '';

    commandsbutton.init = function () {
        _WorkflowId = WF.getWorkflowId();
        _WorktaskId = WF.getWorktaskId();
        _loaded = false;
        WF.commandsbutton.open();
    };

    commandsbutton.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            WorktaskId: _WorktaskId
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            initCommandsButton(data);
        }
    };
    function initCommandsButton(data) {
        if (data.indexOf('ERR%') >= 0) {
            var dataArray = resp.split("@");
            alert("设置流程按钮失败" + dataArray[1]);
        }

    };

    //
    function ButtonItem() {
        //{ text: "保存", handler: handleSubmit1, isDefault: true
        this.text = '';
        this.handler = '';
        this.isDefault = false;
    };
    function formatCommandsButton(data) {
        if (data) {
            var person = JSON.parse(data);
            $.each(person, function (n, value) {
                //do something here   
                //additemtoselect(tbSelecct, value.TableName, value.TableName);
            });
        }

    };
    function createButtonItem(commandName) 
    {
    
    }
    return commandsbutton
})(WF.CommandsButton || {});


WF.TaskBack = (function (taskback) {
    var _baseAPIUrl = "WorkflowDataManager.aspx/taskBackButtonEvent"; //
    if (window.location.pathname == '/Crm/BasePages/WorkTaskCtrlPage.aspx') { _baseAPIUrl = "WorkflowDataManager.aspx/taskBackButtonEvent"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/taskBackButtonEvent"; }
    var _currentParams = [];
    var _loaded = false;

    var _OperatorInsId = "";
    var _WorktaskInsId = "";
    //var _WorktaskId = '';

    taskback.init = function (OperatorInsId) {
        _OperatorInsId = OperatorInsId;
        //_WorktaskId = WF.getWorktaskId();
        _WorktaskInsId = WF.getWorktaskInsId();
        _loaded = false;
        WF.TaskBack.open();
    };

    taskback.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            OperatorInsId: _OperatorInsId
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = resp.split("@");
                alert("退回流程失败" + dataArray[1]);
            }
            else {

                if (_IsIE == '0') {
                    WF.Tooltip.init(WF._TransmitName, _WorktaskInsId);
                } else {
                    window.location = "tooltip.aspx?worktaskInstanceId=" + _WorktaskInsId + " &r=" + Math.random().toString();
                }
            }
        }
    };
    return taskback
})(WF.TaskBack || {});


WF.WFRun = (function (wfrun) {

    var _baseAPIUrl = "WorkflowDataManager.aspx/RunEvent"; //
    if (window.location.pathname == '/Crm/BasePages/WorkTaskCtrlPage.aspx') { _baseAPIUrl = "WorkflowDataManager.aspx/RunEvent"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/RunEvent"; }
    var _currentParams = [];
    var _loaded = false;

    var _OperatorInsId = '';
    var _WorktaskInsId = '';
    var _comName = '';

    wfrun.init = function (OperatorInsId, commandName) {

        _OperatorInsId = OperatorInsId;
        _WorktaskInsId = WF.getWorktaskInsId();
        _comName = commandName;
        _loaded = false;

        WF.WFRun.open();
    };

    wfrun.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            OperatorInsId: _OperatorInsId,
            commandName: _comName
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = resp.split("@");
                alert("提交流程失败" + dataArray[1]);
            }
            else {

                if (_IsIE == '0') {
                    WF.Tooltip.init(WF._TransmitName,_WorktaskInsId);
                } else {
                    window.location = "../crm/BasePages/tooltip.aspx?worktaskInstanceId=" + _WorktaskInsId + " &r=" + Math.random().toString();
                }
            }
        }
    };
    return wfrun
})(WF.WFRun || {});


//指派操作
WF.Assgin = (function (assgin) {

    var _baseAPIUrl = "WorkflowDataManager.aspx/GetWorkTaskAssign"; //
    var _currentParams = [];
    var _loaded = false;

    var _OperatorInsId = '';
    var _WorktaskInsId = '';
    var _assignUser = '';
    //string assignUser, string operatorInsId, string worktaskInsId
    assgin.init = function (assignUser) {
        _OperatorInsId = WF.getOperatorInsId();
        _WorktaskInsId = WF.getWorktaskInsId();
        _assignUser = assignUser;
        _loaded = false;
        WF.Assgin.open();
    };

    assgin.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            assignUser: _assignUser,
            operatorInsId: _OperatorInsId,
            worktaskInsId: _WorktaskInsId
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;

        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = resp.split("@");
                alert("提交流程失败" + dataArray[1]);
            }
            else {
                if (data.indexOf('OK%') >= 0) {
                    var respArray = data.split("@");
                    if (respArray[0] == "OK%") {
                        //WF.Tooltip.init(respArray[1]);
                        window.location = "../crm/BasePages/tooltip.aspx?worktaskInstanceId=" + respArray[1] + " &r=" + Math.random().toString();
                    }
                }
            }
        }
    };
    return assgin
})(WF.Assgin || {});


WF.ReturnControl = (function (returncontrol) {

    var _baseAPIUrl = "WorkflowDataManager.aspx/ReturnControl"; //
    if (window.location.pathname == '/crm/BasePages/StartWorkflow.aspx') { _baseAPIUrl = "WorkflowDataManager.aspx/ReturnControl"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/ReturnControl"; }
    var _currentParams = [];
    var _loaded = false;

    var _OperatorInsId = '';

    //string assignUser, string operatorInsId, string worktaskInsId
    returncontrol.init = function () {
       
        _OperatorInsId = WF.getOperatorInsId();
        _loaded = false;
        WF.ReturnControl.open();
    };

    returncontrol.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            OperatorInsId: _OperatorInsId
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;

        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = resp.split("@");
                alert("获取退回状态失败" + dataArray[1]);
            }
            else {
                if (data.indexOf('OK%') >= 0) {
                    var respArray = data.split("@");
                    if (respArray[0] == "OK%") {
                        //respArray[1];
                        ReturnModel(respArray[1]);
                    }
                }
            }
        }
    };
    return returncontrol
})(WF.ReturnControl || {});

//获取任务实例节点的类型
WF.WorkTaskType = (function (worktask) {

    var _baseAPIUrl = "WorkflowDataManager.aspx/GetWorkTaskTypeID"; //
    if (window.location.pathname == '/Crm/BasePages/WorkTaskCtrlPage.aspx') { _baseAPIUrl = "WorkflowDataManager.aspx/GetWorkTaskTypeID"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/GetWorkTaskTypeID"; }
    var _currentParams = [];
    var _loaded = false;

    var _WorkTaskInstanceId = '';

    worktask.init = function (WorkTaskInstanceId) {

        _WorkTaskInstanceId = WorkTaskInstanceId;
        _loaded = false;
        WF.WorkTaskType.open();
    };

    worktask.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            WorkTaskInstanceId: _WorkTaskInstanceId
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;

        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = resp.split("@");
                alert("获取任务实例节点的类型失败" + dataArray[1]);
            }
            else {
                if (data.indexOf('OK%') >= 0) {
                    var respArray = data.split("@");
                    if (respArray[0] == "OK%") {
                        //respArray[1];
                        WorkTaskStartModel(respArray[1]);
                    }
                }
            }
        }
    };
    return worktask
})(WF.WorkTaskType || {});


WF.UpdateTitle = (function (updatetitle) {

    var _baseAPIUrl = "WorkflowDataManager.aspx/UpdateTitle"; //
    if (window.location.pathname == '/Crm/BasePages/WorkTaskCtrlPage.aspx') { _baseAPIUrl = "WorkflowDataManager.aspx/UpdateTitle"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/UpdateTitle"; }
    var _currentParams = [];
    var _loaded = false;

    var _WorkFlowInstanceId = '';
    var _Title = '';


    updatetitle.init = function (xWorkFlowInstanceId, xTitle) {
        ;
        _WorkFlowInstanceId = xWorkFlowInstanceId;
        _Title = xTitle;
        _loaded = false;
        WF.UpdateTitle.open();
    };

    updatetitle.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            workFlowInstanceId: _WorkFlowInstanceId,
            title: _Title
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;

        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = resp.split("@");
                alert("更新标题失败" + dataArray[1]);
            }
        }
    };
    return updatetitle
})(WF.UpdateTitle || {});


WF.FreeFlow = (function (freeflow) {

    var _baseAPIUrl = "WorkflowDataManager.aspx/FreeFlow"; //

    if (window.location.pathname == '/Crm/BasePages/WorkTaskCtrlPage.aspx' || window.location.pathname == '/crm/BasePages/StartWorkflow.aspx')
    { _baseAPIUrl = "WorkflowDataManager.aspx/FreeFlow"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/FreeFlow"; }
    var _currentParams = [];
    var _loaded = false;

    var _WorkflowId = '';
    var _WorktaskId = '';
    var _Values = '';
    var _Names = '';
    var _ReturnValue = '0';

    //string xWorkflowId, string xWorktaskId, string xValues, string xNames

    freeflow.init = function (xWorkflowId, xWorktaskId, xValues, xNames) {
        ;
        _WorkflowId = xWorkflowId;
        _WorktaskId = xWorktaskId;
        _Values = xValues;
        _Names = xNames;
        _loaded = false;
        WF.FreeFlow.open();
    };

    freeflow.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            xWorkflowId: _WorkflowId,
            xWorktaskId: _WorktaskId,
            xValues: _Values,
            xNames: _Names
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;

        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = resp.split("@");
                _ReturnValue = '1';
                alert("保存不定期失败,申请表单将自动存为草稿！" + dataArray[1]);
            } else { _ReturnValue = '0'; }
        }
    };
    freeflow.getReturnValue = function () {
        return _ReturnValue;
    }
    return freeflow
})(WF.FreeFlow || {});

// 获取指任务实例操作结果
WF.Tooltip = (function (tooltip) {

    var _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/getWFTooltip"; //
    if (window.location.pathname == '/Crm/BasePages/WorkTaskCtrlPage.aspx' || window.location.pathname == '/crm/BasePages/StartWorkflow.aspx')
    { _baseAPIUrl = "WorkflowDataManager.aspx/getWFTooltip"; }
    else { _baseAPIUrl = "../Crm/BasePages/WorkflowDataManager.aspx/getWFTooltip"; }
    var _currentParams = [];
    var _loaded = false;

    var _WorkTaskInstanceId = '';
    var _TitName = '';
    var _Tip = null;

    tooltip.init = function (xTransmitName, xWorkTaskInstanceId) {
        _WorkTaskInstanceId = xWorkTaskInstanceId;
        _TitName = xTransmitName;
        _loaded = false;
        WF.Tooltip.open();
    };

    tooltip.open = function () {

        _load();
    };
    function _load(append) {
        _currentParams = {
            TransmitName: _TitName,
            WorkTaskInstanceId: _WorkTaskInstanceId
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;

        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            var person = JSON.parse(data);
            if (person) {
                var T = person;
                _Tip = WF.NewTip();
                _Tip.title = T.title;   //标题
                _Tip.TaskToWhoMsg = T.TaskToWhoMsg;  //任务信息
                _Tip.ResultMsg = T.ResultMsg;      //操作结果

                WF.setTooltip(_Tip);
            }
        }
    };
    tooltip.getReturnValue = function () {
        return _ReturnValue;
    }
    return tooltip
})(WF.Tooltip || {});

//指定期数记录数据
WF.CrmProductLayout = (function (productlayout) {

    var _baseAPIUrl = "WorkflowDataManager.aspx/GetCrmProductLayout";
    var _currentParams = [];
    var _loaded = false;

    var _ProductLayoutID = ''
    var _BeginDate = '';
    var _EndDate = '';
    var _Place = '';

    productlayout.init = function (xProductLayoutID) {
        _ProductLayoutID = xProductLayoutID;
        WF.CrmProductLayout.open();
    };

    productlayout.open = function () {

        _load();
        // _loaded = false;

    };
    function _load(append) {
        _currentParams = {
            ProductLayoutID: _ProductLayoutID
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;

        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('ERR%') >= 0) {
                var dataArray = data.split("@");
                alert("获取指定期数记录数据失败" + dataArray[1]);
            } else {
                if (data.indexOf('OK%') >= 0) {
                    var respArray = data.split("@");
                    if (respArray[0] == "OK%") {
                        var totalArray = respArray[1].split("|");
                        _BeginDate = totalArray[0];
                        _EndDate = totalArray[1];
                        _Place = totalArray[2];
                    }
                }
            }
        }
    };

    //开始日期
    productlayout.getBeginDate = function () {
        return _BeginDate;
    };
    //结束日期
    productlayout.getEndDate = function () {
        return _EndDate;
    };
    //地点
    productlayout.getPlace = function () {
        return _Place;
    };
    return productlayout
})(WF.CrmProductLayout || {});







